package com.qf.servlet;

import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/10/19 22:34
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/groupon")
public class AdminGrouponRulesController {

    @Autowired
    DtsGrouponRulesService dtsGrouponRulesService;

    /**
     * 查询团购规则列表
     *
     * 业务分析：
     * 1.如果要删除团购规则的信息那么就要将团购记录里面的信息也删除，同时阿里云也要删除
     *    也要将订单表里的团购价格修改
     *
     *
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",defaultValue = "desc") String order,
                       Integer goodsId){

        Map<String,Object> data = dtsGrouponRulesService.findByPage(page,limit,sort,order,goodsId);
        return ResponseUtil.ok(data);

    }

    /**
     * 团购规则添加
     */
    @PostMapping("/create")
    public Object insert(@RequestBody DtsGrouponRules dtsGrouponRules){

        try {
            dtsGrouponRulesService.insert(dtsGrouponRules);
            return ResponseUtil.ok(dtsGrouponRules);
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }
    }

    /**
     * 团购规则修改
     */
    @PostMapping("update")
    public Object update(@RequestBody DtsGrouponRules dtsGrouponRules){

        try {
            dtsGrouponRulesService.update(dtsGrouponRules);
            return ResponseUtil.ok();
        }catch (Exception e) {
           e.printStackTrace();
           return ResponseUtil.fail(-1,e.getMessage());
        }
    }
    /**
     * 团购规则删除
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGrouponRules dtsGrouponRules){
        dtsGrouponRulesService.delete(dtsGrouponRules);
        return ResponseUtil.ok();

    }
}
